<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px"
      style="width: 80%; margin-left:50px;" :disabled="dialogStatus == 'look'">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="temp.jobName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务状态" prop="checkStatus">
            <el-radio v-for="item in dictList('is_use')" :key="'type' + item.label" v-model="temp.checkStatus"
              :label="item.value" :value="item.value" >
              {{ item.label }}
            </el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据抽取名称" prop="extractionName">
            <el-select v-model="temp.extractionName" class="filter-item" placeholder="请选择执行器" @change="choseDraw">
              <el-option v-for="(item, index) in drawlist" :key="index" :label="item.extractionName" :value="item" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="执行器" prop="appName">
            <el-select v-model="temp.appName" class="filter-item" placeholder="请选择执行器" @change="choseJob">
              <el-option v-for="(item, index) in fastlist" :key="index" :label="item.appName" :value="item" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="cron表达式" prop="cronExpression">
            <el-input v-model="temp.cronExpression" @focus="dialogCron = true" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="执行参数" prop="methodParams">
            <el-input v-model="temp.methodParams" />
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="dialogStatus === 'create' ? createData() : updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogCron" :append-to-body="true"
      :close-on-click-modal="false">
      <Crontab @fill="getStr" @hide="dialogCron = false"></Crontab>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { createDataExtractionTask, updateDataExtractionTask, getDataExtractionTask,getmetadataJob } from '@/api/dataextractiontask/dataExtractionTask'
import { fetchDataExtractionList } from '@/api/dataextractionrealtime/dataExtraction'
import Crontab from "@/components/Crontab/index.vue"
import { fetchDataExtractionGroupList } from '@/api/dataextractiongroup/dataExtractionGroup'
export default {
  name: 'DataExtractionTaskForm',
  components: { Crontab },
  data() {
    return {
      rules: {
        extractionName: [{ required: true, message: '数据抽取名称为必填项', trigger: 'blur' }],
        appName: [{ required: true, message: '任务名称为必填项', trigger: 'blur' }],
        checkStatus: [{ required: true, message: '任务状态为必填项', trigger: 'blur' }],
        cronExpression: [{ required: true, message: 'cron表达式为必填项', trigger: 'blur' }],
        jobName: [{ required: true, message: '名称为必填项', trigger: 'blur' }]
      },
      temp: {},
      textMap: {
        update: '编辑',
        create: '新建',
        look:'查看'
      },
      listQuery: {
        page: 1,
        limit: 999
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      dialogCron: false,
      fastlist:[],
      drawlist:[]
    }
  },
  created(){
    fetchDataExtractionGroupList(this.listQuery).then(res=>{
            this.fastlist=res.data.data
    })
    fetchDataExtractionList(this.listQuery).then(response => {
        this.drawlist = response.data.data
      })
  },
  methods: {
    choseDraw(val){
     this.temp.extractionName=val.extractionName
     this.temp.dataExtractionId=val.id
    },
    choseJob(val){
      this.temp.appName=val.appName
      this.temp.jobGroup=val.id
      this.temp.executeClass=val.executeClass
      this.temp.methodName=val.executeMethod
    },
    getList() {
      this.$emit('refreshList')
    },
    //获取返回的cron表达式
    getStr(str) {
      this.temp.cronExpression = str
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        jobName: undefined,
        checkStatus: undefined,
        checkDate: undefined,
        createBy: undefined,
        createDate: undefined,
        updateDate: undefined,
        updateBy: undefined,
        delFlag: undefined,
        tenantId: undefined,
        cronExpression: undefined,
        remarks: undefined,
        jobGroup: undefined,
        misfirePolicy: undefined,
        loadWay: undefined,
        description: undefined,
        isConcurrent: undefined,
        executeClass: undefined,
        methodName: undefined,
        methodParams: undefined,
        dataExtractionId: undefined
      }
    },
    setList(id){
      getmetadataJob(id).then(response => {
        if (response.data.code === 0) {
          this.temp = response.data.data
          this.list=[]
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }

      })
     },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleLook(id){
      console.log(id)
      this.id = id
      this.resetTemp()
      this.dialogStatus = 'look'
      this.dialogFormVisible = true
      this.setList(id)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.temp)
          // this.loading = true
          // createDataExtractionTask(this.temp).then(response => {
          //   this.loading = false
          //   if (response.data.code === 0) {
          //     this.getList()
          //     this.dialogFormVisible = false
          //     this.$message.success(response.data.msg)
          //   } else {
          //     this.$message.error(response.data.msg)
          //   }
          // })
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getDataExtractionTask(id).then(response => {
        if (response.data.code === 0) {
          this.temp = response.data.data
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          updateDataExtractionTask(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
